package com.yanq.liancar.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.bean.loginUserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BLoginActivity extends AppCompatActivity {


    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.forget_password)
    TextView forgetPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.register)
    TextView register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blogin);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
    }

    private void initOkGo() {
        OkGo.<String>get(Config.s + "llc/admin/loginBUser")
                .tag(this)
                .params("tel", phoneNum.getText().toString().trim())
                .params("password", password.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        loginUserBean bean = gson.fromJson(response.body(), loginUserBean.class);
                        if (bean.isIsSuccess()) {
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            // 隐藏软键盘
                            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                            LogUtils.e("1111111", bean.getData().getToken());
                            UserBean.setbID(BLoginActivity.this, bean.getData().getUser().getUid());
                            UserBean.setbToken(BLoginActivity.this, bean.getData().getToken());
                            UserBean.setbName(BLoginActivity.this, bean.getData().getUser().getUsername());
                            UserBean.setbSign(BLoginActivity.this, bean.getData().getUser().getSign());
                            UserBean.setbAgreement(BLoginActivity.this, bean.getData().getProtocol());
                            if (UserBean.getbAgreement(BLoginActivity.this)!= 0) {
                                Bundle bundle=new Bundle();
                                bundle.putInt("type",1);
                                RxActivityTool.skipActivity(BLoginActivity.this, ProtocolListActivity.class,bundle);
                            } else {
                                Intent intent = new Intent(BLoginActivity.this, ServerMainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                            UserBean.setbAuthority(BLoginActivity.this, bean.getData().getUser().getHaveFCWStatus());
                            finish();
                            //RxActivityTool.skipActivityAndFinish(BLoginActivity.this, ServerMainActivity.class);
                            RxToast.success(bean.getOutputMessage());
                            pushPhoneToken();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }

                });
    }

    @OnClick({R.id.back_btn, R.id.forget_password, R.id.tv_login, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.forget_password:
                startActivity(new Intent(this, BForgetPwdActivity.class));
                break;
            case R.id.tv_login:
                initOkGo();
                //                if (Utils.isMobile(phoneNum.getText().toString().trim())){
                //                    initOkGo();
                //                }else {
                //                    RxToast.error("请输入正确电话号码");
                //                }
                break;
            case R.id.register:
                startActivity(new Intent(this, RegesterActivity.class));
                break;
        }
    }

    private void pushPhoneToken() {
        OkGo.<String>post(Config.s + "llc/pushController/addPushTokenForUser")
                .tag(this)
                .params("token", UserBean.getbToken(this))
                .params("pushToken", UserBean.getPushToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean baseBean = gson.fromJson(response.body(), BaseBean.class);
                        if (baseBean.isIsSuccess()) {
                        } else {
                            RxToast.error(baseBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        RxToast.error("服务器连接失败");
                        LogUtils.e("asdasdad", response.code() + "");
                        LogUtils.e("asdasdad", response.message() + "");
                        super.onError(response);
                    }
                });
    }
}
